package com.tronsis.library.imageloader;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.tronsis.library.R;

import java.io.File;



/**
 * 图片加载类
 *
 * @author allen@tronsis.com
 * @date 7/5/2016 7:04 PM
 * @update scofield@tronsis.com
 * @date 2018/6/6
 *
 */
public class ImageLoader {

    /**
     * 显示图片
     *
     * url/model为 null 时，可以设置.fallback()区分error
     *
     * @param context
     * @param imageUrl
     * @param iv
     */
    public static void display(Context context, String imageUrl, ImageView iv) {
        GlideApp.with(context)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.color.main_bg)
                .error(R.color.main_bg)
                .into(iv);
    }

    public static void displayAvatar(Context context, String imageUrl, ImageView iv) {
        GlideApp.with(context)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.icon_user_default)
                .error(R.drawable.icon_user_default)
                .into(iv);
    }

    /**
     * 显示图片
     *
     * @param context
     * @param imageUrl
     * @param iv
     */
    public static void display(Fragment context, String imageUrl, ImageView iv) {
        GlideApp.with(context)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.color.main_bg)
                .error(R.color.main_bg)
                .into(iv);
    }

    /**
     * 显示图片
     *
     * @param context
     * @param imageUrl
     * @param iv
     */
    public static void display(Activity context, String imageUrl, ImageView iv) {
        GlideApp.with(context)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.color.main_bg)
                .error(R.color.main_bg)
                .into(iv);
    }

    public static void display(Context context, Bitmap bitmap, ImageView iv) {
        GlideApp.with(context)
                .load(bitmap)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.color.main_bg)
                .error(R.color.main_bg)
                .into(iv);
    }

    public static void displayNoCache(Context context, Bitmap bitmap, ImageView iv) {
        GlideApp.with(context)
                .load(bitmap)
                .signature(new StringSignature(String.valueOf(System.currentTimeMillis()))) //防止同个url不更新的问题
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(iv);
    }

    /**
     * 显示图片
     *
     * @param context
     * @param file
     * @param iv
     */
    public static void display(Context context, File file, ImageView iv) {
        GlideApp.with(context)
                .load(file)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .placeholder(R.color.main_bg)
                .error(R.color.main_bg)
                .into(iv);
    }

    /**
     * 显示图片
     *
     * @param context
     * @param uri
     * @param iv
     */
    public static void display(Context context, Uri uri, ImageView iv) {
        GlideApp.with(context)
                .load(uri)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.color.main_bg)
                .error(R.color.main_bg)
                .into(iv);
    }

    /**
     * 显示圆角图片
     *
     * @param context
     * @param imageUrl
     * @param iv
     * @param roundRadiusDp
     */
    public static void displayRoundImage(Context context, String imageUrl, ImageView iv, int roundRadiusDp) {
       /* GlideApp.with(mContext)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(mContext, roundRadiusDp))
                .error(R.color.main_bg)
                .placeholder(R.color.main_bg)
                .into(iv);*/
    }


    /**
     * 清除图片磁盘缓存
     */
    public static void clearImageDiskCache(final Context context) {
        try {
            if (Looper.myLooper() == Looper.getMainLooper()) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.get(context)
                                .clearDiskCache();
                    }
                });
            } else {
                Glide.get(context)
                        .clearDiskCache();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除图片内存缓存
     */
    public static void clearImageMemoryCache(Context context) {
        try {
            //只能在主线程执行
            if (Looper.myLooper() == Looper.getMainLooper()) {
                Glide.get(context)
                        .clearMemory();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
